//
//  IteratorPattern .swift
//  AQDeal
//
//  Created by zhangjikuan on 2020/6/11.
//  Copyright © 2020 hsgd. All rights reserved.
//

/// Iterator 迭代器 Behavior Patterns


import Foundation
public struct Queue<T> {
    private var array:[T?] = []
    
    private var head = 0
    
    public var isEmpty: Bool {
        return count == 0
    }
    
    public var count: Int {
        return array.count - head
    }
    
    public mutating func enqueue(_ element: T) {
        array.append(element)
    }
    
    public mutating func dequeue() -> T? {
        guard head < array.count ,
            let element = array[head] else {
                return nil
        }
        
        array[head] = nil
        head += 1
        
        let persentage = Double(head)/Double(array.count)
        
        if array.count > 50,
            persentage > 0.25 {
            array.removeFirst()
            head = 0
        }
        return element
        
    }
}

extension Queue: Sequence {
    public func makeIterator() -> IndexingIterator<ArraySlice<T?>> {
        // 只枚举非空对象
        let nonEmptyValues = array[head ..< array.count]
        return nonEmptyValues.makeIterator()
    }
}

public struct Ticket {
    var description:String
    var priority:PriorityType
    
    enum PriorityType {
        case low
        case medium
        case high
    }
    
    init(description: String, priority: PriorityType) {
        self.description = description
        self.priority = priority
    }
    
    
}
extension Ticket {
    var sortIndex: Int {
        switch self.priority {
        case .low:
            return 0
        case .medium:
            return 1
        case .high:
            return 2
        }
    }
}


class TestPattern: NSObject {
    func testO() {
        
        var queue = Queue<Ticket>()
        
        queue.enqueue(Ticket(description:"Wireframe Tinder for dogs app", priority: .low))
        queue.enqueue(Ticket(description:"Set up 4k monitor for Josh", priority: .medium))
        queue.enqueue(Ticket(description:"There is smoke coming out of my laptop", priority: .high))
        queue.enqueue(Ticket(description:"Put googly eyes on the Roomba", priority: .low))
        
        let element = queue.dequeue()
        
        print((element?.description ?? "No Description") + "\n")
        
        
        // 枚举 queue 并输出
        print("List of Tickets in queue:")
        for ticket in queue {
            print(ticket?.description ?? "No Description")
        }
        
        // 进行排序
        let sortedTickets = queue.sorted {
            $0!.sortIndex > ($1?.sortIndex)!
        }
        
        // 循环 sortedTickets 数组，将元素添加到 sortedQueue
        var sortedQueue = Queue<Ticket>()
        for ticket in sortedTickets {
            sortedQueue.enqueue(ticket!)
        }
        
        // 输出 sotedQueue 内对象，可以看到 first in, firt out。
        print("\n")
        print("Tickets sorted by priority:")
        for ticket in sortedQueue {
            print(ticket?.description ?? "No Description")
        }
    }
  
}
